home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / networke / xnetmine.tgz / XNetMine / XNetMine.c < prev   
C/C++ Source or Header  |  1994-09-26  |  50KB  |  1,384 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <X11/X.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/Xutil.h>
  6. #include <X11/Xatom.h>
  7. #include <X11/Xos.h>
  8. #include <X11/Xresource.h>
  9. /*** Sockets include *****/
  10. #include <errno.h>      /* obligatory includes */
  11. #include <signal.h> 
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #include <unistd.h> 
  15. #include <sys/socket.h>
  16. #include <sys/time.h> 
  17. #include <sys/wait.h> 
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20.  
  21.  
  22.  
  23. #define X_MAX    31        /* wert+1 = maximale anzahl steine in x            */ 
  24. #define Y_MAX    17        /* dto. nur fuer y                                 */
  25. #define BOMB_MAX 80        /* wieviele Steine maximal auf dem Spielfeld sind */
  26.  
  27. #define LEER      0            
  28. #define SHOW      18
  29. #define LOCK      20
  30. #define HIDE      17
  31. #define NIX       21
  32. #define BOMB      19
  33. #define EXBOMB    22
  34.  
  35. #define max_msg   4 
  36. #define SWIDTH    20
  37. #define SHEIGHT   20
  38. #define bigx      20
  39. #define bigy      20
  40. #define BWIDTH    14
  41. #define XOFFSET   10
  42. #define YOFFSET   60
  43. #define BHEIGHT   14
  44.  
  45. struct verbindungen {                                        /* struktur der verbindungsliste     */
  46.                           int    kanal_nummer;                 /* Kanal-nummer                      */
  47.                           struct verbindungen *nextptr;          /* zeiger auf naechstes element      */
  48.                            char Was;                    /* ='W' write to server    ='R' read to server */
  49.                        };
  50.  
  51. short int Feld_Intern[X_MAX+X_MAX*Y_MAX+10];
  52. short int Feld_Visible[X_MAX+X_MAX*Y_MAX+10];
  53. short int Feld_Backup[X_MAX+X_MAX*Y_MAX+10];
  54.  
  55. struct AnfragePaket
  56.  {
  57.      short int Feld_Visible[X_MAX+X_MAX*Y_MAX+10];
  58.      short int Feld_Intern[X_MAX+X_MAX*Y_MAX+10];
  59.      char UserName[100];
  60.      char Was;                    /* ='W' write to server    ='R' read to server */
  61.  };
  62.  
  63. int t,i,z,u,v,w,x,y,xx,yy,weiter_msg;
  64. int spiel_laeuft,err;
  65. Display *display;
  66. Window  window;
  67. struct verbindungen *next;
  68. char fontname[80] = "8x13bold";
  69. char *Display_name = NULL; 
  70. unsigned long bg; 
  71. int sock_server,sock_client,screen;
  72. int s1,s2,max,HighScore;
  73. Window  hswindow = 0;
  74. XEvent event;
  75. XSizeHints hint;
  76. GC gc,hsgc;
  77. GC highlightgc;
  78. XFontStruct *xfontinfo;
  79. int height;
  80. int highest_kanal_number ;
  81. int width;
  82. int Server,Client;
  83. char msg[max_msg+5][120];
  84. char ServerName[200],User[200],Port[200],text[100],winner[100];
  85. KeySym  key;
  86. Colormap cmap;
  87. XColor exact_def;
  88. unsigned long foreground, background,patbg,patfg,b1,b2,b3,b4,b5,b6,b7,b8,gray,white,black;
  89. int depth;
  90. Pixmap graypix;
  91. Pixmap minepix;
  92. Pixmap minepix_a;   
  93. Pixmap coveredpix;
  94. Pixmap smileypix;
  95. Pixmap copyrightpix;
  96. Pixmap netpix;
  97. Pixmap newpix;
  98. Pixmap lockpix;
  99.  
  100. char lock_bits[] = {
  101.  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x3f, 0x00, 0xe0, 0x3f, 0x00,
  102.   0xe0, 0x3f, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x07, 0x00, 0x00, 0x02, 0x00,
  103.   0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x00,
  104.   0x00, 0x02, 0x00, 0x00, 0x07, 0x00, 0xc0, 0x1f, 0x00, 0xe0, 0x3f, 0x00,
  105.   0xe0, 0x3f, 0x00, 0xe0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106.   };
  107.  
  108.  
  109. char new_bits[] = {
  110.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff,
  111.   0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  112.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff,
  113.   0xff, 0xff, 0xff, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f,
  114.   0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0xcf, 0xff, 0xff, 0xff,
  115.   0xff, 0xff, 0xcf, 0x0f, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x0f,
  116.   0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x0f, 0xcf, 0xff, 0xff, 0xff,
  117.   0xff, 0xff, 0xcf, 0x0f, 0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f,
  118.   0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f, 0xcf, 0x03, 0x00, 0x00,
  119.   0x00, 0x80, 0xcf, 0x0f, 0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f,
  120.   0xcf, 0xc3, 0xf0, 0xbf, 0x3b, 0x87, 0xcf, 0x0f, 0xcf, 0x83, 0x21, 0x21,
  121.   0x11, 0x82, 0xcf, 0x0f, 0xcf, 0x83, 0x21, 0x01, 0x33, 0x82, 0xcf, 0x0f,
  122.   0xcf, 0x83, 0x22, 0x11, 0x22, 0x81, 0xcf, 0x0f, 0xcf, 0x83, 0x24, 0x1f,
  123.   0x76, 0x81, 0xcf, 0x0f, 0xcf, 0x83, 0x24, 0x11, 0x54, 0x81, 0xcf, 0x0f,
  124.   0xcf, 0x83, 0x28, 0x01, 0xcc, 0x80, 0xcf, 0x0f, 0xcf, 0x83, 0x30, 0x21,
  125.   0x88, 0x80, 0xcf, 0x0f, 0xcf, 0xc3, 0xa1, 0x3f, 0x88, 0x80, 0xcf, 0x0f,
  126.   0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f, 0xcf, 0x03, 0x00, 0x00,
  127.   0x00, 0x80, 0xcf, 0x0f, 0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f,
  128.   0xcf, 0x03, 0x00, 0x00, 0x00, 0x80, 0xcf, 0x0f, 0xcf, 0xff, 0xff, 0xff,
  129.   0xff, 0xff, 0xcf, 0x0f, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x0f,
  130.   0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x0f, 0xcf, 0xff, 0xff, 0xff,
  131.   0xff, 0xff, 0xcf, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f,
  132.   0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0xff, 0xff, 0xff, 0xff,
  133.   0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  134.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff,
  135.   0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  136.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, };
  137.  
  138. static unsigned char net_bits[] = {
  139.  0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x22, 0x22, 0x22, 0x22,
  140.   0x22, 0x22, 0x22, 0x02, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x04,
  141.   0x88, 0x88, 0x88, 0x08, 0x88, 0x88, 0x88, 0x08, 0x44, 0x44, 0x44, 0x44,
  142.   0x44, 0x44, 0x44, 0x04, 0x22, 0x22, 0x22, 0x22, 0x02, 0x22, 0x22, 0x02,
  143.   0x11, 0x11, 0x01, 0x11, 0x11, 0x10, 0x11, 0x01, 0x22, 0x02, 0x22, 0x22,
  144.   0x22, 0x02, 0x22, 0x02, 0x44, 0x04, 0x40, 0x40, 0x40, 0x44, 0x44, 0x04,
  145.   0x80, 0x00, 0x80, 0x00, 0x08, 0x80, 0x88, 0x08, 0x44, 0x00, 0x00, 0x00,
  146.   0x00, 0x00, 0x44, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x02,
  147.   0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x11, 0x01, 0x22, 0x02, 0x1e, 0xe7,
  148.   0xf7, 0x07, 0x22, 0x02, 0x44, 0x00, 0x33, 0x42, 0x92, 0x04, 0x44, 0x04,
  149.   0x88, 0x00, 0x21, 0x42, 0x82, 0x00, 0x88, 0x08, 0x44, 0x80, 0x40, 0x42,
  150.   0x82, 0x00, 0x44, 0x04, 0x22, 0x80, 0x40, 0x42, 0x82, 0x00, 0x22, 0x02,
  151.   0x11, 0x80, 0x40, 0x42, 0x82, 0x00, 0x11, 0x01, 0x00, 0x02, 0x21, 0x42,
  152.   0x82, 0x00, 0x20, 0x02, 0x44, 0x00, 0x33, 0x66, 0x82, 0x00, 0x44, 0x04,
  153.   0x88, 0x00, 0x1e, 0x3c, 0xc7, 0x01, 0x88, 0x08, 0x44, 0x04, 0x18, 0x00,
  154.   0x00, 0x00, 0x44, 0x04, 0x22, 0x00, 0x60, 0x00, 0x00, 0x00, 0x22, 0x02,
  155.   0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x01, 0x22, 0x02, 0x00, 0x00,
  156.   0x00, 0x20, 0x02, 0x02, 0x44, 0x04, 0x00, 0x00, 0x00, 0x40, 0x44, 0x04,
  157.   0x88, 0x00, 0x00, 0x00, 0x00, 0x80, 0x88, 0x08, 0x44, 0x44, 0x00, 0x40,
  158.   0x00, 0x40, 0x44, 0x04, 0x22, 0x22, 0x20, 0x20, 0x22, 0x22, 0x22, 0x02,
  159.   0x11, 0x01, 0x01, 0x11, 0x11, 0x10, 0x11, 0x01, 0x22, 0x22, 0x22, 0x22,
  160.   0x22, 0x22, 0x22, 0x02, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x04,
  161.   0x88, 0x08, 0x88, 0x88, 0x80, 0x88, 0x88, 0x08, 0x44, 0x44, 0x44, 0x44,
  162.   0x44, 0x44, 0x44, 0x04, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x02,
  163.   0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x22, 0x22, 0x22, 0x22,
  164.   0x22, 0x22, 0x22, 0x02, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x00,
  165.   0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x08, };
  166.  
  167.  
  168. char copyright_bits[] = {
  169.   0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  170.   0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  171.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x10, 0x10, 0x10, 0x10,
  172.   0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
  173.   0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0xff, 0xff, 0x7f, 0xfb,
  174.   0xf8, 0xff, 0xff, 0x0f, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01,
  175.   0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
  176.   0x01, 0x01, 0x01, 0x01, 0xff, 0xff, 0x7e, 0x62, 0x40, 0xff, 0xff, 0x0f,
  177.   0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,
  178.   0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00,
  179.   0xff, 0xff, 0x1a, 0x01, 0x00, 0xfa, 0xff, 0x0f, 0x01, 0x01, 0x01, 0xe0,
  180.   0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0xb0, 0x03, 0x01, 0x01, 0x01,
  181.   0x01, 0x01, 0x00, 0x18, 0x02, 0x00, 0x01, 0x01, 0xff, 0xff, 0x03, 0x08,
  182.   0x00, 0xf8, 0xff, 0x0f, 0x10, 0x10, 0x00, 0x08, 0x00, 0x10, 0x10, 0x00,
  183.   0x10, 0x10, 0x10, 0x08, 0x02, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x18,
  184.   0x02, 0x10, 0x10, 0x00, 0xff, 0xff, 0x05, 0x30, 0x03, 0xff, 0xff, 0x0f,
  185.   0x01, 0x01, 0x01, 0xe0, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
  186.   0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01,
  187.   0xff, 0xff, 0x35, 0x01, 0x00, 0xf9, 0xff, 0x0f, 0x10, 0x10, 0x10, 0x00,
  188.   0x00, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00,
  189.   0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0xff, 0xff, 0xff, 0xff,
  190.   0xff, 0xff, 0xff, 0x0f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  191.   0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  192.   0x01, 0x01, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  193.   0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10,
  194.   0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
  195.   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, };
  196.  
  197. char covered_bits[] = {
  198.    0x00, 0x00, 0x08, 0x00, 0x00, 0x0c, 0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e,
  199.    0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e, 0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e,
  200.    0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e, 0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e,
  201.    0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e, 0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e,
  202.    0x54, 0x55, 0x0d, 0xa8, 0xaa, 0x0e, 0xfe, 0xff, 0x0f, 0xff, 0xff, 0x0f};
  203.  
  204. char gray_bits[] = {
  205.    0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a, 0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a,
  206.    0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a, 0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a,
  207.    0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a, 0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a,
  208.    0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a, 0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a,
  209.    0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a, 0x55, 0x55, 0x05, 0xaa, 0xaa, 0x0a};
  210.  
  211. char mine_bits[] = {
  212.    0xff, 0xff, 0x0f, 0x01, 0x00, 0x08, 0x01, 0x02, 0x08, 0x01, 0x02, 0x08,
  213.    0x91, 0x4f, 0x08, 0xe1, 0x3f, 0x08, 0xe1, 0x3f, 0x08, 0x71, 0x7e, 0x08,
  214.    0x71, 0x7e, 0x08, 0xfd, 0xff, 0x09, 0xf1, 0x7f, 0x08, 0xf1, 0x7f, 0x08,
  215.    0xe1, 0x3f, 0x08, 0xe1, 0x3f, 0x08, 0x91, 0x4f, 0x08, 0x01, 0x02, 0x08,
  216.    0x01, 0x02, 0x08, 0x01, 0x00, 0x08, 0x01, 0x00, 0x08, 0xff, 0xff, 0x0f};
  217.  
  218. char smiley_bits[] = {
  219.    0xff, 0xff, 0x0f, 0x01, 0x00, 0x0c, 0x81, 0x0f, 0x0c, 0x61, 0x30, 0x0c,
  220.    0x11, 0x40, 0x0c, 0x09, 0x80, 0x0c, 0x09, 0x80, 0x0c, 0x45, 0x10, 0x0d,
  221.    0x05, 0x00, 0x0d, 0x05, 0x00, 0x0d, 0x05, 0x00, 0x0d, 0x25, 0x20, 0x0d,
  222.    0xc9, 0x98, 0x0c, 0x09, 0x87, 0x0c, 0x11, 0x40, 0x0c, 0x61, 0x30, 0x0c,
  223.    0x81, 0x0f, 0x0c, 0x01, 0x00, 0x0c, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f};
  224.  
  225. void draw_screen();
  226. void male_gitter();
  227. void draw_item();
  228. unsigned long getcolor();
  229.  
  230. /***********************************/
  231.  
  232. int reads(socket,ptr,size)
  233. int socket;
  234. char *ptr;
  235. int size;
  236. { int s,t,r;
  237.   r=0; t=0; s=0;
  238.  for (;1==1;)
  239.  {
  240.   t++;
  241.   if (t>(size+3)) return(r);
  242.   s=read(socket,ptr,size-r); 
  243.   ptr=ptr+s;
  244.   r=r+s;
  245.   if (r==-1) { return(0); }
  246.   if (r==size) return(size);
  247.  }
  248. }
  249.  
  250.  
  251.  int call_socket(hostname, portnum)
  252.  
  253.   char *hostname;
  254.   long portnum;
  255.   { struct sockaddr_in sa;
  256.     struct hostent     *hp;
  257.     int a, s;
  258.  
  259.     if ((hp= gethostbyname(hostname)) == NULL) { /* do we know the host's */
  260.       errno= ECONNREFUSED;                       /* address? */
  261.       return(-1);                                /* no */
  262.     }
  263.  
  264.     bzero(&sa,sizeof(sa));
  265.     bcopy(hp->h_addr,(char *)&sa.sin_addr,hp->h_length); /* set address */
  266.     sa.sin_family= hp->h_addrtype;
  267.     sa.sin_port= htons((u_short)portnum);
  268.     if ((s= socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)   /* get socket */
  269.       return(-1);
  270.     if (connect(s,(struct sockaddr *)&sa,sizeof sa) < 0)                    /* connect */
  271.       return(-1);
  272.     return(s);
  273.   }
  274.  
  275. int establish(portnum)                                         /* funktion stellt einen socket zur */
  276.   u_short portnum;                                             /* zur verfuegung                   */
  277.  
  278.  {  char   myname[100];                                        /* diverse variablen                */
  279.     int    s;
  280.     struct sockaddr_in sa;
  281.     struct hostent *hp;
  282.  
  283.     bzero(&sa,sizeof(struct sockaddr_in));                     /* strukutr saubermachen           */
  284.     gethostname(myname,99);                                    /* unserer eigener hostname        */
  285.     hp= gethostbyname(myname);                                 /* ... infos ueber uns bekommen    */
  286.     if (hp == NULL)                                            /* gib es uns wirklich ?           */
  287.        {perror("Keine Hostadresse gefunden"); exit(-1); };     /* nein, scream and die ....       */
  288.     sa.sin_family= hp->h_addrtype;                             /* unsere host addresse            */
  289.     sa.sin_port= htons(portnum);                               /* unsere port nummer              */
  290.     if ((s= socket(AF_INET,SOCK_STREAM,0)) < 0)                /* und einen socket machen         */
  291.        {perror("Socket not installed"); exit(-1);};      /* kein socket => scream and die   */
  292.     if (bind(s,(struct sockaddr *) &sa,sizeof sa) < 0)         /* adresse dem socket mitteilen    */
  293.      { printf("\n XNetBomb-Server is already running !\n"); exit(-1);};   /* binde addresse zum socket       */
  294.     listen(s, 5 );                                             /* warteschlagne = 5 plaetze       */
  295.     return(s);                                                 /* und unseren socket zurueckgeben */
  296.  }
  297.  
  298.  
  299. alte_verbindung_loeschen(verbindung_next) /* loescht einen Kanal aus der Verbindungs-liste */
  300. struct verbindungen *verbindung_next;
  301. {
  302.  
  303. verbindung_next->Was=9;
  304. return;
  305. fflush(stdout);
  306.   if (verbindung_next==next)                                   /* sind wir erstes element ?       */
  307.      {                                                         /* ja,wir sind erstes element      */
  308.        next=verbindung_next->nextptr;
  309.        free(verbindung_next);
  310.      }
  311.    else                                                        /* nein, wir sind nicht erstes element */
  312.      { struct verbindungen *Ptr;
  313.        for (Ptr=next;Ptr!=NULL;) 
  314.        { if (Ptr->nextptr==verbindung_next)
  315.          {
  316.            Ptr->nextptr=verbindung_next->nextptr;
  317.            free(verbindung_next);
  318.           return;
  319.          }
  320.            Ptr=Ptr->nextptr;
  321.        }
  322.      }
  323. }
  324.  
  325.  
  326.  
  327. void Sende_to_Client()
  328. {          long res,max,t,err,size;
  329.            struct verbindungen *verbindung_next;
  330.            struct timeval timeout;
  331.            fd_set writefds;
  332.            struct AnfragePaket Anfrage;
  333.            Anfrage.Was=0;
  334.            strcpy( Anfrage.UserName,"SERVER");
  335.            max=X_MAX*Y_MAX+6; 
  336.            timeout.tv_sec=0;
  337.            timeout.tv_usec=900;
  338.            for (t=0;t<=max;t++)
  339.            {
  340.              Anfrage.Feld_Visible[t]=Feld_Visible[t];
  341.              Anfrage.Feld_Intern[t]=Feld_Intern[t];
  342.              if (Anfrage.Feld_Intern[t]==EXBOMB) Anfrage.Feld_Intern[t]=BOMB;
  343.            }
  344.            verbindung_next=next;                                            /* listenstart setzen                       */
  345.            for (;(verbindung_next!=NULL)&&(next!=NULL);)                    /* und bis zum letzten element machen ...   */
  346.             {
  347.               if (verbindung_next->Was=='R')
  348.               {
  349.                 size=sizeof(struct AnfragePaket);
  350.                 FD_ZERO(&writefds);
  351.                 FD_SET(verbindung_next->kanal_nummer,&writefds);
  352.                 err=0;
  353.                 res = select(verbindung_next->kanal_nummer+10,0,&writefds,0,&timeout);          
  354.                 if (res==1) { err=write(verbindung_next->kanal_nummer,&Anfrage,size); } 
  355.                 if (err!=size) {KillKanal(verbindung_next); }
  356.                 verbindung_next=verbindung_next->nextptr; 
  357.  
  358.               } else { verbindung_next=verbindung_next->nextptr; }
  359.             }   
  360. }
  361.  
  362. void neue_verbindung_einfuegen(wert,kanal_nummer)                  /* fuegt ein Kanal in die Verbindungsliste ein */
  363. char wert;
  364. int kanal_nummer;
  365. {
  366.   struct verbindungen *Ptr;
  367.   Ptr=(struct verbindungen *)malloc(sizeof(struct verbindungen));
  368.   if (Ptr==NULL) { printf("\n Error, no memory "); exit(-1); }
  369.   Ptr->kanal_nummer=kanal_nummer;
  370.    Ptr->nextptr=next;
  371.   Ptr->Was=wert;
  372.  
  373.   next=Ptr;
  374. }
  375.  
  376.  
  377. KillKanal(Ptr)
  378.  struct verbindungen *Ptr;
  379.  close ((int) Ptr->kanal_nummer);
  380.  
  381.  alte_verbindung_loeschen(Ptr);
  382. }
  383.  
  384. void Play_Server_Game()
  385. {
  386.   if (XPending(display))      
  387.   {
  388.    getXevent();    /* bearbeite event */
  389.   }
  390. }
  391.  
  392. void other_player_win(a)
  393. int a;
  394. {
  395. if (a==0)
  396. { /* client zeigt an */
  397.   spiel_laeuft=0;
  398.   strcpy(text,"Another Player win the game. Serverplayer will restart the game now.");
  399.   XSetForeground(display, gc, getcolor("gray",foreground));
  400.   XFillRectangle(display,window,gc,4,385,625,400);
  401.   strcpy(winner,text);
  402.   XSetForeground(display, gc, getcolor("blue",foreground));
  403.   XDrawString(display,window,gc,10,395 ,text,strlen(text));
  404. } else
  405. { /* Server zeigt an */
  406.   spiel_laeuft=0;
  407.   strcpy(text,"Another Player win the game. Please RESTART NOW the game.");
  408.   strcpy(winner,text);
  409.   XSetForeground(display, gc, getcolor("gray",foreground));
  410.   XFillRectangle(display,window,gc,4,385,625,400);
  411.   XSetForeground(display, gc, getcolor("blue",foreground));
  412.   XDrawString(display,window,gc,10,395 ,text,strlen(text));
  413.   Feld_Visible[X_MAX*Y_MAX+3]=77;
  414.   if (Server==1) Sende_to_Client();                     
  415. }
  416. }
  417.  
  418. int get_connection(s)                                       /* warte auf eine verbindungsversuch und verwalte die */
  419. int s;                                                      /* restlichen Lese/schreibaufforderungen ....         */
  420.  {
  421.     struct verbindungen *verbindung_next;
  422.     struct verbindungen *verbindung_neu;
  423.     struct sockaddr_in isa;                                   /* addresse des sockets                               */
  424.     char *buffer[10];
  425.     int size,i,err,change;                                    /* i=groesse der adresse                              */
  426.     int res,t,new_socket;                                     /* new_socket = socket of connection                  */
  427.     fd_set readfds;
  428.     fd_set writefds;
  429.     int nfds,okk;
  430.     struct timeval timeout;
  431.     FD_ZERO(&readfds);
  432.     change=1;
  433.     i = sizeof(isa);                                                     /* socket addresse finden                            */
  434.     getsockname(s,(struct sockaddr *) &isa,&i);                          /*                                                   */
  435.     while (1==1)              /* endlos machen */
  436.      {
  437.       Play_Server_Game();
  438.       timeout.tv_sec=0;
  439.       timeout.tv_usec=500;
  440.       FD_SET(s ,&readfds );
  441.       res=select(highest_kanal_number+10,&readfds,0,0,&timeout);               /* warte auf connect bzw. 100usec                    */
  442.       if ((FD_ISSET(s ,&readfds)) && (res>0))                                             /* war da ein connect ?                              */
  443.        {                                                                  /* ok, hier haben wir einen neuen requester          */
  444.         i = sizeof(isa);                                                     /* socket addresse finden                            */
  445.         getsockname(s,(struct sockaddr *) &isa,&i);                      
  446.         new_socket =accept(s ,(struct sockaddr *) &isa ,&i );           /* neuer socket in die liste aufnehmen               */
  447.         if (new_socket>=highest_kanal_number) {highest_kanal_number=new_socket+10;};  /* gleiche obergrenze fuer fd-files an     */
  448.         if (new_socket==-1) { exit(-1); }  
  449.         if (new_socket != -1) 
  450.          {  
  451.           struct AnfragePaket Anfrage;
  452.           size=sizeof(struct AnfragePaket);
  453.           FD_ZERO(&readfds);
  454.           FD_SET(new_socket ,&readfds );
  455.           timeout.tv_sec=10;
  456.           err=0;
  457.           timeout.tv_usec=900;
  458.           res = select(new_socket+3, &readfds, 0, 0, &timeout);
  459.           if (res==1) { err=reads(new_socket,&Anfrage,size); }
  460.           if (err==size) 
  461.            {
  462.             if (Anfrage.Was=='W') neue_verbindung_einfuegen('W',new_socket);
  463.             if (Anfrage.Was=='R') neue_verbindung_einfuegen('R',new_socket);  
  464.             Sende_to_Client();
  465.            }
  466.          }   
  467.        } 
  468.       verbindung_next=next;
  469.       FD_ZERO(&readfds);                                                              
  470.       timeout.tv_sec=0;                                                               
  471.       timeout.tv_usec=500;                                                            
  472.       okk=0; 
  473.       while (verbindung_next!=NULL)                                                    
  474.        {
  475.         if (verbindung_next->Was=='W') { okk=1; FD_SET(verbindung_next->kanal_nummer,&readfds); }  
  476.         verbindung_next=verbindung_next->nextptr;                                 
  477.        }
  478.       res=-1;
  479.       if (okk==1) 
  480.        {
  481.          res = select(highest_kanal_number, &readfds, 0, 0, &timeout);
  482.         }                 
  483.       if (res)                                                                 
  484.        {
  485.        verbindung_next=next;                                        
  486.         while ((verbindung_next!=NULL)&&(next!=NULL))                   
  487.          {
  488.           if (FD_ISSET(verbindung_next->kanal_nummer,&readfds))                  
  489.            { 
  490.             struct AnfragePaket Anfrage;
  491.             size=sizeof(struct AnfragePaket);
  492.             err=reads(verbindung_next->kanal_nummer,&Anfrage,size);
  493.             if (err!=size) { KillKanal(verbindung_next); }
  494.             if (err==size)
  495.              {
  496.                long max,t;
  497.                 max=X_MAX*Y_MAX; 
  498.      
  499.               if (Anfrage.Feld_Visible[max+3]==77)
  500.                 {
  501.                  spiel_laeuft=0;
  502.                  other_player_win(1);                        
  503.                 } 
  504.        
  505.               for (t=0;t<=max;t++)
  506.                 {
  507.                  if ((Anfrage.Feld_Visible[t]==SHOW) && (Feld_Visible[t]==HIDE))
  508.                   { 
  509.                    Feld_Visible[t]=SHOW; 
  510.                    change=1;
  511.                   }
  512.                 } 
  513.               
  514.              } 
  515.            } 
  516.           if (verbindung_next!=NULL) verbindung_next=verbindung_next->nextptr;
  517.          } 
  518.        } 
  519.        if (change==1) 
  520.         { 
  521.          Sende_to_Client();
  522.          for (y=1;(y<Y_MAX);y++)
  523.           {                    
  524.            for (x=1;x<X_MAX;x++)   
  525.             {
  526.              if (Feld_Visible[x+y*X_MAX]!=Feld_Backup[x+y*X_MAX])
  527.               {
  528.                draw_item(x,y);
  529.               }
  530.             } 
  531.           }                   
  532.          male_gitter();  
  533.         }  
  534.        change=0;
  535.      } 
  536.  
  537.  
  538.  
  539. unsigned long getcolor(s,monocolor)
  540.     char *s;
  541.     unsigned long monocolor;
  542. {
  543.     if(depth != 1)
  544.     {
  545.         XParseColor(display,cmap,s,&exact_def);
  546.         XAllocColor(display,cmap,&exact_def);
  547.         return exact_def.pixel;
  548.     }
  549.     else
  550.     {
  551.         return monocolor;
  552.     }
  553. }
  554.  
  555. void spielfeld_init()
  556. {
  557.  
  558. short int zufall_x,zufall_y; 
  559. int x,y,xx,yy;
  560. spiel_laeuft=1;
  561.  
  562. /* Init des Spielfeldes */
  563.  
  564. for (y=0;y<=Y_MAX;y++)
  565. {
  566.  for (x=0;x<=X_MAX;x++)
  567.  {
  568.   Feld_Intern [x+y*X_MAX]=0;
  569.   Feld_Visible[x+y*X_MAX]=HIDE;
  570.   Feld_Backup[x+y*X_MAX]=NIX;
  571.  }
  572. }
  573.  
  574.   Feld_Intern [Y_MAX*X_MAX+3]=0;
  575.   Feld_Visible[Y_MAX*X_MAX+3]=HIDE;
  576.   Feld_Backup [Y_MAX*X_MAX+3]=NIX;
  577.   Feld_Intern [Y_MAX*X_MAX+4]=0;
  578.   Feld_Visible[Y_MAX*X_MAX+4]=HIDE;
  579.   Feld_Backup [Y_MAX*X_MAX+4]=NIX;
  580.  
  581. for  (y=0;y<=Y_MAX;y++)
  582. {
  583.  Feld_Visible[y*X_MAX]=SHOW;
  584.  Feld_Visible[y*X_MAX+X_MAX]=SHOW;
  585. }
  586. for  (x=0;x<=X_MAX;x++)
  587.  Feld_Visible[x]=SHOW;
  588.  Feld_Visible[Y_MAX*X_MAX+x]=SHOW;
  589. }
  590.  
  591.  
  592. /* Verteilung der Bomben */
  593. srand(time(NULL));           /* init der Random-zahlen */
  594.  
  595. for (t=1;t!=BOMB_MAX;t++)
  596. {
  597.  zufall_x=1+(rand() % (X_MAX-1));
  598.  zufall_y=1+(rand() % (Y_MAX-1));
  599.  Feld_Intern[zufall_x+zufall_y*X_MAX]=BOMB;  
  600. }
  601.  
  602.  
  603. /* Durchzaehlung , wo die Bomben liegen */
  604.  
  605. for (y=1;y<Y_MAX;y++)    
  606. {                         
  607.  for (x=1;x<=X_MAX;x++)   
  608.  {                        
  609.   if (BOMB!=Feld_Intern[x+y*X_MAX])
  610.   {
  611.    t=0;
  612.    if (BOMB==Feld_Intern[x-1+(y-1)*X_MAX]) t++;      /* element oben links   */
  613.    if (BOMB==Feld_Intern[x-1+(y  )*X_MAX]) t++;      /* element links        */
  614.    if (BOMB==Feld_Intern[x-1+(y+1)*X_MAX]) t++;      /* element unten links  */
  615.    if (BOMB==Feld_Intern[x+1+(y-1)*X_MAX]) t++;      /* element oben rechts  */ 
  616.    if (BOMB==Feld_Intern[x+1+(y  )*X_MAX]) t++;      /* element rechts       */
  617.    if (BOMB==Feld_Intern[x+1+(y+1)*X_MAX]) t++;      /* element rechts unten */
  618.    if (BOMB==Feld_Intern[x  +(y+1)*X_MAX]) t++;      /* element unten        */
  619.    if (BOMB==Feld_Intern[x  +(y-1)*X_MAX]) t++;      /* element oben         */
  620.    Feld_Intern[x+y*X_MAX]=t;  
  621.   }                        
  622.  }          
  623. }
  624.  
  625. }
  626.  
  627. void E()
  628. {
  629. printf("\n Parameters are: -Server -Client -Name.... -PortNumber.... -ServerName.... -NoNet\n");
  630. printf("\n (1) if you are the server :  -Server -PortNumber..... -Name..... ");
  631. printf("\n (2) if you are the client :  -Client -PortNumber..... -ServerName..... -Name..... ");
  632. printf("\n     Server-Programm MUST be running !");
  633. printf("\n (3) for a single user game:  -NoNet -Name.... \n");
  634. printf("\n .... stands for a string\n\n\n"); 
  635. printf("\n(C) by Martin Bauer\nif you want to contact me write a email to:");
  636. printf("\nInternet-Adress: Martin_Bauer@S2.MAUS.DE\n\n");
  637. fflush(stdout); exit(-1);
  638. }
  639.  
  640.  
  641. void check_command(argc,argv)
  642. int argc;
  643. char *argv[];
  644. {
  645. Server=1;
  646. Client=1;
  647. strcpy(Port,"");
  648. strcpy(ServerName,"");
  649. strcpy(User,"");
  650. if (argc>1)
  651. {
  652.  for (t=0;t<(argc-1);t++)
  653.  {
  654.   if (strcmp("-Server",argv[t+1])==0) 
  655.   {
  656.     Server=1;
  657.     Client=0;
  658.   }
  659.    if (strcmp("-Client",argv[t+1])==0)
  660.   {
  661.    Server=0;
  662.    Client=1;
  663.   }
  664.   if (strcmp("-NoNet",argv[t+1])==0)
  665.   {
  666.    Server=0;
  667.    Client=0; 
  668.   }
  669.   if (strncmp("-PortNumber",argv[t+1],11)==0)
  670.   { char text[500];
  671.     strcpy(text,argv[t+1]);
  672.     strcpy(Port,&text[11]);
  673.   }
  674.   if (strncmp("-Name",argv[t+1],5)==0)
  675.   {
  676.     char text[500];
  677.     strcpy(text,argv[t+1]);
  678.     strcpy(User,&text[5]);
  679. }
  680.   if (strncmp("-ServerName",argv[t+1],11)==0)
  681.   {
  682.     char text[500];
  683.     strcpy(text,argv[t+1]);
  684.     strcpy(ServerName,&text[11]);
  685.   }
  686.  }
  687. } else { printf("\n ERROR - not enough paramters\n\n"); fflush(stdout); E(); }
  688. if ((Client==1) && (Server==1)) { printf("\n ERROR   -Client or -Server flag is missing ! \n\n"); E(); }
  689. if ((Client==0) && (Server==0)) { if (strcmp(User,"")==0) {printf("\n ERROR - UserName is missing ! \n\n"); E(); } }
  690. printf("\n Server:%d  Client:%d  PortNum:%s \n",Server,Client,Port);
  691. printf(" Name:\"%s\"  ServerName:\"%s\"\n",User,ServerName);fflush(stdout);
  692. }
  693.  
  694. void CheckForClientEvent();
  695.  
  696. void sigi()
  697. {
  698.  signal(SIGPIPE,sigi);
  699.  
  700. }
  701.  
  702. main(argc,argv)
  703. int argc;
  704. char *argv[];
  705. {   char into[600];
  706.     next=NULL;
  707.     strcpy(winner,"it's not a sony, it's freeware  :=)");
  708.     weiter_msg=0;  
  709.     strcpy(msg[0],"May your children and mine live in peace");
  710.     strcpy(msg[1],"This is not a fault, this is a feature !");
  711.     strcpy(msg[2],"If you are female... write me ... soon !");
  712.     strcpy(msg[3],"Dont hit any mines !");
  713.     strcpy(msg[4],"Please write me if you enjoy this game");
  714.     signal(SIGPIPE,sigi);
  715.  
  716.    highest_kanal_number=20;
  717.  
  718.     check_command(argc,argv);
  719.     spielfeld_init();
  720.  
  721.     if(!(display = XOpenDisplay(Display_name)))
  722.     {
  723.         printf("Error: can't open display\n");
  724.         exit(1);
  725.     }
  726.  
  727.  
  728.     screen = DefaultScreen(display);
  729.     depth  = DefaultDepth(display,screen) ; 
  730.     cmap = DefaultColormap(display,screen); 
  731.     background = WhitePixel(display,screen);
  732.     foreground = BlackPixel(display,screen);
  733.     bg = getcolor("gray",background); 
  734.  
  735.     hint.x = 0; hint.y = 0;
  736.     hint.width = 622;
  737.     hint.height = 400;
  738.     hint.flags = PPosition | PSize;
  739.     window = XCreateSimpleWindow (display,
  740.                 DefaultRootWindow(display),
  741.                 hint.x, hint.y, hint.width, hint.height,
  742.                 5, foreground, background);
  743.     sprintf(into,"XNetMines  [%s]",User);  
  744.     XSetStandardProperties(display, window,into, into,
  745.         None, argv, argc, &hint);
  746.   
  747.     gc = XCreateGC(display, window, 0, 0);
  748.     XSetBackground(display, gc, background);
  749.     XSetForeground(display, gc, foreground);
  750.     highlightgc = XCreateGC(display, window, 0, 0);
  751.  
  752.      XSelectInput(display, window,
  753.         Button1MotionMask | Button2MotionMask | Button3MotionMask |
  754.          ButtonPressMask | ButtonReleaseMask |
  755.         KeyPressMask | ExposureMask);
  756.  
  757.  
  758.  
  759.     if((xfontinfo = XLoadQueryFont(display,fontname)) == NULL)
  760.     {
  761.         fprintf(stderr,"Can't find font '%s'\n",fontname);
  762.         xfontinfo = XQueryFont(display,XGContextFromGC(gc));
  763.     }
  764.     XSetFont(display,gc,xfontinfo->fid);
  765.  
  766.     b1=getcolor("blue",foreground);
  767.     b2=getcolor("green",foreground);
  768.     b3=getcolor("red",foreground);
  769.     b4=getcolor("orange",foreground);
  770.     b5=getcolor("violett",foreground);
  771.     b6=getcolor("cyan",foreground);
  772.     b7=getcolor("white",foreground);
  773.     b8=getcolor("magenta",foreground);
  774.     black=getcolor("black",foreground);
  775.     gray =getcolor("gray",foreground);
  776.     white=getcolor("white",foreground);
  777.  
  778.  
  779.     smileypix = XCreatePixmapFromBitmapData(display,window,
  780.                      smiley_bits,SWIDTH,SHEIGHT,foreground,
  781.                      background, depth);
  782.  
  783.     graypix = XCreatePixmapFromBitmapData(display,window,
  784.                     gray_bits,SWIDTH,SHEIGHT,foreground,
  785.                     background, depth);
  786.  
  787.     coveredpix = XCreatePixmapFromBitmapData(display,window,
  788.                     covered_bits,SWIDTH,SHEIGHT,foreground,
  789.                     background, depth);     
  790.  
  791.    lockpix = XCreatePixmapFromBitmapData(display,window,
  792.                     lock_bits,SWIDTH,SHEIGHT,foreground,
  793.                     background, depth);
  794.  
  795.   
  796.     minepix = XCreatePixmapFromBitmapData(display,window,
  797.                 mine_bits,SWIDTH,SHEIGHT,foreground,
  798.                 background, depth);
  799.  
  800.    netpix = XCreatePixmapFromBitmapData(display,window,
  801.                 net_bits,60,40,b1,
  802.                 background, depth);
  803.  
  804.    copyrightpix = XCreatePixmapFromBitmapData(display,window,
  805.                 copyright_bits,60,40,b1,
  806.                 background, depth);
  807.  
  808.    newpix = XCreatePixmapFromBitmapData(display,window,
  809.                 new_bits,60,40,b1,
  810.                 background, depth);
  811.     HighScore=0;
  812.     if(depth == 1)    
  813.     {
  814.         XSetWindowBackgroundPixmap(display,window,graypix);
  815.     }
  816.      
  817.     minepix_a = XCreatePixmapFromBitmapData(display,window,
  818.                 mine_bits,SWIDTH,SHEIGHT,foreground,
  819.                 b3, depth);
  820.  
  821.  
  822.     XMapRaised(display, window);
  823.     XFlush(display);
  824.     draw_screen();
  825.  
  826.     if ((Server==0) && (Client==0)) { CheckforEvent(); }
  827.  
  828. if (Server==1)
  829. { long PortNumber;
  830.  if (strcmp(Port,"")==0)       {printf("\n ERROR - PortNumber is missing ! \n\n"); E(); }
  831.  if (strcmp(User,"")==0)       {printf("\n ERROR - UserName   is missing ! \n\n"); E(); }
  832.  if (0==sscanf(Port,"%ld",&PortNumber)) { printf("\n ERROR - PortNumber is wrong ! \n\n"); E(); } 
  833.  sock_server=establish(PortNumber);
  834.  get_connection(sock_server);     /* und endlosschleife */
  835.  }
  836.  
  837. if (Client==1)
  838. { long PortNumber;
  839.     fd_set readfds;
  840.     struct timeval timeout;
  841.  if (strcmp(ServerName,"")==0) {printf("\n ERROR - ServerName is missing ! \n\n"); E(); }
  842.  if (strcmp(Port,"")==0)       {printf("\n ERROR - PortNumber is missing ! \n\n"); E(); }
  843.  if (strcmp(User,"")==0)       {printf("\n ERROR - UserName   is missing ! \n\n"); E(); }
  844.  if (0==sscanf(Port,"%ld",&PortNumber)) { printf("\n ERROR - PortNumber is wrong ! \n\n"); E(); }
  845.  s1=call_socket(ServerName,PortNumber);
  846.  if (s1==-1)
  847.  {
  848.    printf("\nError: Socketerror\n"); E();  
  849.  }
  850.  
  851.  {
  852.   long max,t;
  853.   struct AnfragePaket Anfrage;
  854.   strcpy( Anfrage.UserName,User);
  855.   max=X_MAX*Y_MAX;
  856.   for (t=0;t<=max;t++)
  857.     {
  858.       Anfrage.Feld_Visible[t]=Feld_Visible[t];
  859.       Anfrage.Feld_Intern[t] =Feld_Intern[t];
  860.     }
  861.   Anfrage.Was='W';                     /* s1 ist der kanal ZUM SCHREIBEN */
  862. timeout.tv_sec=10;
  863. timeout.tv_usec=0;
  864. FD_ZERO(&readfds);
  865. FD_SET(s1,&readfds);
  866. if (1==select(s1+1,0,&readfds,0,&timeout))
  867.     { 
  868.     write(s1,&Anfrage,sizeof(struct AnfragePaket));
  869.     }
  870.     else
  871.     {
  872.     printf("Error: Server-Timeout V1.0\n");
  873.     exit(-1);
  874.     }
  875.  
  876.   Anfrage.Was='R';                     /* s2 ist der kanal zum LESEN */
  877.   s2=call_socket(ServerName,PortNumber);
  878. timeout.tv_sec=10;
  879. timeout.tv_usec=0;
  880. FD_ZERO(&readfds);
  881.  if (s2==-1) {printf("\nError: Socketerror\n"); E(); }
  882. FD_SET(s2,&readfds);
  883. if (1==select(s2+1,0,&readfds,0,&timeout))
  884.     {
  885.   write(s2,&Anfrage,sizeof(struct AnfragePaket));
  886.      } 
  887.     else
  888.     {
  889.     printf("Error: Server-Timeout V2.0\n");
  890.     exit(-1);
  891.     }
  892.  CheckForClientEvent();  
  893.  }
  894. }
  895. }
  896.  
  897. void show_end()
  898. {
  899.   int xx,yy,nr,x,y;
  900.  
  901.   for (y=1;(y<Y_MAX-1);y++)
  902.   {                    
  903.    for (x=1;x<X_MAX;x++)   
  904.    {
  905.  
  906.   xx=x*bigx;
  907.   yy=y*bigy;
  908.   nr=x+y*X_MAX;
  909.  
  910.   if ((Feld_Intern[nr]==BOMB))
  911.    {
  912.      XCopyArea(display,minepix,window,gc,0,0,SWIDTH,SHEIGHT,xx-10,40+yy);
  913.    }
  914.  
  915.    if ((Feld_Visible[nr]==SHOW) && (Feld_Intern[nr]==EXBOMB))
  916.    {
  917.      XCopyArea(display,minepix_a,window,gc,0,0,SWIDTH,SHEIGHT,xx-10,40+yy);
  918.    }
  919.  
  920.   if ((Feld_Visible[nr]==LOCK) && (Feld_Intern[nr]!=BOMB))
  921.    { 
  922.      XSetForeground(display, gc, b3);
  923.      XDrawLine(display,window,gc,xx-10+bigx-3,40+yy,xx-10-3,40+yy+bigy);
  924.      XDrawLine(display,window,gc,xx-10,40+yy,xx-10+bigx,40+yy+bigy);
  925.    }
  926. }
  927. }
  928.   male_gitter();
  929. }
  930.  
  931. void CheckForClientEvent()
  932. { int max,res,x,y,xx,yy,size,i,err,change;    
  933.   fd_set readfds;
  934.   struct timeval timeout;
  935.   struct AnfragePaket Anfrage;
  936.  
  937.   for (;1==1;)
  938.   {
  939.    FD_ZERO(&readfds);
  940.    FD_SET(s2,&readfds );
  941.    timeout.tv_sec=0; 
  942.    timeout.tv_usec=800;
  943.    res = select(s2+1, &readfds, 0, 0, &timeout);
  944.    if ((res!=-1) && (FD_ISSET(s2,&readfds))) 
  945.    { /* aha, server schickt uns daten */
  946.      err=reads(s2,&Anfrage,sizeof(struct AnfragePaket));
  947.      if (err!=sizeof(struct AnfragePaket)) {  printf("\n Socket-Datenfehler \n"); exit(-1);}
  948.      max=X_MAX*Y_MAX;
  949.      if ((Anfrage.Feld_Visible[Y_MAX*X_MAX+3]==77) && (Feld_Visible[Y_MAX*X_MAX+3]!=77))
  950.      {  spiel_laeuft=0;
  951.          /* anderer Player hat gewonnen */
  952.         other_player_win(0);
  953.      }
  954.      
  955.      if (Anfrage.Feld_Intern[Y_MAX*X_MAX+4]==99) 
  956.      {
  957.       weiter_msg++;
  958.       if (weiter_msg>max_msg) weiter_msg=0;  
  959.       strcpy(winner,msg[weiter_msg]);
  960.   
  961.       spiel_laeuft=1;HighScore=0;
  962.       for (t=0;t<=(max+4);t++)
  963.       { 
  964.         Feld_Visible[t]=HIDE;
  965.         Feld_Intern[t]=0;
  966.        }
  967.        draw_screen(); 
  968.      }
  969.     for (t=0;t<=max;t++)
  970.      {
  971.      if (spiel_laeuft==1)
  972.      {
  973.       if ((SHOW==Anfrage.Feld_Visible[t])&&(Feld_Intern[t]!=BOMB)) Feld_Visible[t]=SHOW;
  974.       if (Feld_Intern[t]!=EXBOMB) Feld_Intern[t]=Anfrage.Feld_Intern[t];
  975.      }
  976.        }
  977.           for (y=1;(y<Y_MAX);y++)
  978.           {
  979.             for (x=1;x<X_MAX;x++)
  980.             {
  981.             
  982.              if (Feld_Visible[x+y*X_MAX]!=Feld_Backup[x+y*X_MAX])
  983.               {
  984.                draw_item(x,y);
  985.               }
  986.             }
  987.           }
  988.           male_gitter();
  989.        }
  990.   if (XPending(display))      
  991.   {
  992.    getXevent();    /* bearbeite event */
  993.   }
  994.  }
  995. }
  996.  
  997. CheckforEvent()
  998. {   
  999.  for (;1==1;)
  1000.  {    
  1001.  /*  if (XPending(display))          => nur wenn wir noch was machen wollen */
  1002.   {                                       
  1003.    getXevent();                      
  1004.   }
  1005.  }
  1006. }
  1007.  
  1008. void draw_item(x,y)
  1009. int x,y;
  1010. {
  1011.   char a;
  1012.   int xx,yy,nr;
  1013.   a='.';
  1014.   xx=x*bigx;
  1015.   yy=y*bigy;
  1016.   nr=x+y*X_MAX;
  1017.   XSetForeground(display, gc, gray);
  1018.   XFillRectangle(display,window,gc,xx-10,40+yy,20,20); 
  1019.   Feld_Backup[nr]=Feld_Visible[nr];
  1020.   if (Feld_Visible[nr]==SHOW)
  1021.   {
  1022.    if (Feld_Intern[nr]==0)    { a='.'; }
  1023.    if (Feld_Intern[nr]==1)    { a='1'; XSetForeground(display, gc, b1); }
  1024.    if (Feld_Intern[nr]==2)    { a='2'; XSetForeground(display, gc, b2); }
  1025.    if (Feld_Intern[nr]==3)    { a='3'; XSetForeground(display, gc, b3); }
  1026.    if (Feld_Intern[nr]==4)    { a='4'; XSetForeground(display, gc, b4); }
  1027.    if (Feld_Intern[nr]==5)    { a='5'; XSetForeground(display, gc, b5); }
  1028.    if (Feld_Intern[nr]==6)    { a='6'; XSetForeground(display, gc, b6); }
  1029.    if (Feld_Intern[nr]==7)    { a='7'; XSetForeground(display, gc, b7); }
  1030.    if (Feld_Intern[nr]==8)    { a='8'; XSetForeground(display, gc, b8); }
  1031.    if (Feld_Intern[nr]==EXBOMB) 
  1032.    { a='.';  
  1033.     XCopyArea(display,minepix_a,window,gc,0,0,SWIDTH,SHEIGHT,xx-10,40+yy);
  1034.    }
  1035.  
  1036.   if (Feld_Intern[nr]==BOMB)
  1037.    {
  1038.        XCopyArea(display,minepix,window,gc,0,0,SWIDTH,SHEIGHT,xx-10,40+yy);
  1039.        a='.';  
  1040.    }
  1041.    if (a!='.')
  1042.    { char text[4];
  1043.      text[0]=a; text[1]=0;
  1044.      XDrawString(display,window,gc,xx-4,54+yy,text,1);
  1045.    }
  1046.   }
  1047.   if (Feld_Visible[nr]==HIDE)
  1048.   { 
  1049.      xx=x*bigx-8;
  1050.      yy=y*bigy+42;
  1051.      XSetForeground(display, gc, white);
  1052.      XFillRectangle(display,window,gc,xx-2,yy-2,20,20);
  1053.      XSetForeground(display, gc, gray);
  1054.      XFillRectangle(display,window,gc,xx,yy,18,18);
  1055.   }
  1056.  
  1057.  if (Feld_Visible[nr]==LOCK)
  1058.   { 
  1059.    XCopyArea(display,lockpix,window,gc,0,0,SWIDTH,SHEIGHT,x*bigx-10,39+y*bigy);
  1060.   }
  1061. }
  1062.  
  1063. void male_gitter()
  1064. {
  1065.    int i,x,y,xx,yy;
  1066.    XSetForeground(display, gc, black);
  1067.    for (i=0;i<2;i++)
  1068.    {
  1069.     for (y=0;y<(Y_MAX);y++)     
  1070.     {
  1071.      yy=y*bigy;
  1072.      XDrawLine(display,window,gc,bigx-12,yy+58+i,X_MAX*bigx-bigx+9,58+yy+i);
  1073.     }
  1074.     for (x=0;x<(X_MAX);x++)
  1075.     {
  1076.      xx=x*bigx;
  1077.      XDrawLine(display,window,gc,i+xx+8,60,i+xx+8,38+Y_MAX*bigy);
  1078.     }
  1079.    }
  1080. }
  1081.  
  1082.  
  1083. void draw_screen()
  1084.   long dir,xx,yy,x,y,a,b,c,d;
  1085.   GC highlightgc,shadowgc;
  1086.  
  1087.   XSetForeground(display, gc, white);
  1088.   XFillRectangle(display,window,gc,9,60,bigx*X_MAX-bigx,bigy*Y_MAX);
  1089.   XSetBackground(display, gc, gray);
  1090.   XSetForeground(display, gc, gray);
  1091.   XFillRectangle(display,window,gc,0,0,1280,59);
  1092.   XFillRectangle(display,window,gc,0,0,9,1000);
  1093.   XFillRectangle(display,window,gc,8+(X_MAX-1)*bigx,0,800,1000);
  1094.   XFillRectangle(display,window,gc,0,60+(Y_MAX-1)*bigy,800,1000);
  1095.   XSetForeground(display, gc, gray);
  1096.   for (y=1;y<Y_MAX;y++)
  1097.   {
  1098.    for (x=1;x<X_MAX;x++)
  1099.    {
  1100.      xx=x*bigx-8;
  1101.      yy=y*bigy+42;
  1102.      XFillRectangle(display,window,gc,xx,yy,18,18);
  1103.   }
  1104.   }
  1105.   for (y=1;(y<Y_MAX);y++)
  1106.   {                    
  1107.    for (x=1;x<X_MAX;x++)   
  1108.    {
  1109.     if (Feld_Visible[x+y*X_MAX]!=HIDE)
  1110.     {
  1111.      draw_item(x,y);
  1112.     }
  1113.    }
  1114.   }
  1115.   XSetForeground(display, gc, black);
  1116.   XCopyArea(display,copyrightpix,window,gc,0,0,60,40,10,10);
  1117.   XCopyArea(display,netpix,window,gc,0,0,60,40,550,10);
  1118.   if (Client!=1) XCopyArea(display,newpix,window,gc,0,0,60,40,270,10);
  1119.   male_gitter();
  1120.   if (spiel_laeuft==0) show_end();
  1121.     sprintf(text,"Score: %d",HighScore);
  1122.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1123.                        XFillRectangle(display,window,gc,75,0,60,50);
  1124.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1125.                        XDrawString(display,window,gc,75,30 ,text,strlen(text));
  1126.                   
  1127.  
  1128.                 XSetForeground(display, gc, getcolor("gray",foreground));
  1129.                        XFillRectangle(display,window,gc,4,383,625,400);
  1130.                        XSetForeground(display, gc, getcolor("red",foreground));
  1131.                     XDrawString(display,window,gc,10,395,winner,strlen(winner));
  1132.        
  1133.  
  1134. }
  1135.  
  1136.  
  1137. void Fuelle(x,y)
  1138. int x,y;
  1139. {
  1140. xx=x+y*X_MAX;
  1141. Feld_Visible[xx]=SHOW;
  1142. draw_item(x,y);
  1143. if (Feld_Intern[xx]==0)
  1144.  { 
  1145.    if (Feld_Visible[x+y*X_MAX+1    ]==HIDE) Fuelle(x+1,y);
  1146.    if (Feld_Visible[x+y*X_MAX-1    ]==HIDE) Fuelle(x-1,y);
  1147.    if (Feld_Visible[x+y*X_MAX+X_MAX]==HIDE) Fuelle(x,y+1);
  1148.    if (Feld_Visible[x+y*X_MAX-X_MAX]==HIDE) Fuelle(x,y-1);
  1149.  }
  1150. }
  1151.  
  1152. Client_to_Server()
  1153. {   struct AnfragePaket Anfrage;
  1154.                        Anfrage.Was='W'; 
  1155.                        strcpy( Anfrage.UserName,User);
  1156.                        max=X_MAX*Y_MAX+4;
  1157.                        for (t=0;t<=max;t++)
  1158.                        {
  1159.                          Anfrage.Feld_Visible[t]=Feld_Visible[t];
  1160.                          Anfrage.Feld_Intern[t]=Feld_Intern[t];
  1161.                        }
  1162.     err=write(s1,&Anfrage,sizeof(struct AnfragePaket));
  1163.     if (err!=sizeof(struct AnfragePaket)) { printf("\n socket-write fehler \n"); exit(-1); }
  1164. }                    
  1165.  
  1166. getXevent()                                 
  1167. {
  1168.     XNextEvent(display, &event);
  1169.     switch(event.type)
  1170.     {
  1171.         case Expose:
  1172.             if(event.xexpose.window == window)
  1173.             {
  1174.                 if(event.xexpose.count == 0)
  1175.                 {
  1176.                     /* window wurde gemoved , also neu aufbauen */
  1177.                     draw_screen();
  1178.                 }
  1179.             }
  1180.             break;                          
  1181.         case ButtonPress:                      
  1182.             if(event.xkey.window == window)
  1183.             {
  1184.                 x = (event.xbutton.x-XOFFSET)/SWIDTH;
  1185.                 y = (event.xbutton.y-YOFFSET)/SHEIGHT;
  1186.                 xx=event.xbutton.x;
  1187.                 yy=event.xbutton.y;
  1188.                 if ((yy>=10) && (yy<=40))
  1189.                 {
  1190.                   if ((xx>10) && (xx<70))
  1191.                   { /* Copyright angeklickt */
  1192.                        char text[120];
  1193.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1194.                        XFillRectangle(display,window,gc,4,383,625,400);
  1195.                        XSetForeground(display, gc, getcolor("red",foreground));
  1196.                        strcpy(text,"(c) 1994 by Martin Bauer    to contact me: Martin_Bauer@S2.MAUS.DE");
  1197.                        strcpy(winner,text);   
  1198.                     XDrawString(display,window,gc,10,395,text,strlen(text));
  1199.                   }
  1200.                  
  1201.                   if ((xx>270) && (xx<330) && (Client!=1))
  1202.                   { /* new angeklickt */
  1203.  
  1204.                     spielfeld_init();
  1205.                     weiter_msg++;
  1206.                      if (weiter_msg>max_msg) weiter_msg=0;  
  1207.   
  1208.                     strcpy(winner,msg[weiter_msg]);
  1209.  
  1210.                     HighScore=0;
  1211.                     draw_screen(); 
  1212.                     Feld_Intern[Y_MAX*X_MAX+4]=99;
  1213.                     Sende_to_Client();           
  1214.                     Feld_Intern[Y_MAX*X_MAX+4]=0;
  1215.                  }
  1216.  
  1217.                   if ((xx>550) && (xx<610))
  1218.                   { /* net-config angeklickt */
  1219.                     if (Server==1) close(sock_server);
  1220.                     if (Client==1) close(s1);
  1221.                     if (Client==1) close(s2);
  1222.                     exit(-1);
  1223.                   }
  1224.                 } 
  1225.                if ((x>=0) && (x<(X_MAX-1)) && (y>=0) && (y<(Y_MAX-1)) && (spiel_laeuft==1))
  1226.                 {    /* spieler hat im Spielfeld geklickt ! */
  1227.                   x++; y++;
  1228.              if (event.xbutton.button==Button1)
  1229.              {    /* taste 1 im spielfeld geklickt ! */
  1230.                   xx=x+y*X_MAX; 
  1231.                   if (Feld_Visible[xx]==HIDE)
  1232.                   {  /* hat auf verstecktes feld getippt */
  1233.                      Feld_Visible[xx]=SHOW;
  1234.                      if (Feld_Intern[xx]==0)    { Fuelle(x,y); }
  1235.  
  1236.                      if ((Feld_Intern[xx]!=BOMB) && (Client==1))
  1237.                      {  /* ok, jetzt dem server mitteilen */
  1238.                        long max,t,err,size;
  1239.                        struct AnfragePaket Anfrage;
  1240.                        Anfrage.Was='W'; 
  1241.                        strcpy( Anfrage.UserName,User);
  1242.                        max=X_MAX*Y_MAX;
  1243.                        for (t=0;t<=max;t++)
  1244.                        {
  1245.                          Anfrage.Feld_Visible[t]=Feld_Visible[t];
  1246.                          Anfrage.Feld_Intern[t]=Feld_Intern[t];
  1247.                        }
  1248.                      err=write(s1,&Anfrage,sizeof(struct AnfragePaket));
  1249.                      if (err!=sizeof(struct AnfragePaket)) { printf("\n socket-write fehler \n"); exit(-1); }
  1250.                      }
  1251.                      if ((Feld_Intern[xx]!=BOMB) && (Server==1))
  1252.                      {  /* ok, jetzt den clients mitteilen */
  1253.                       Sende_to_Client();                       
  1254.                      }
  1255.                      if (Feld_Intern[xx]==BOMB) 
  1256.                      { char text[80];
  1257.                        spiel_laeuft=0;
  1258.                        Feld_Intern[xx]=EXBOMB;
  1259.                        strcpy(text,"Booooooommmmmm , you lost the game. Please restart the game now.");
  1260.                        if (Client==1) strcpy(text,"You lost the game. This game will be restart after one Player is left");
  1261.                        if (Server==1) strcpy(text,"You lost the game. Please DO NOT restart the Game NOW. ");
  1262.                        if ((Server==1) && (next==NULL))  strcpy(text,"You lost the game. Please RESTART the Game NOW. ");
  1263.  
  1264.                        strcpy(winner,text); 
  1265.                       XSetForeground(display, gc, getcolor("gray",foreground));
  1266.                        XFillRectangle(display,window,gc,4,385,625,400);
  1267.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1268.                        XDrawString(display,window,gc,10,395 ,text,strlen(text));
  1269.                        show_end(); 
  1270.                       }
  1271.                      if ((Feld_Intern[xx]!=BOMB))
  1272.                      {  /* user hat ein zahlenfeld/space aufgedeckt ! */
  1273.                        int k;
  1274.                        k=0;
  1275.                        if (Feld_Intern[xx]<=8) HighScore=HighScore+Feld_Intern[xx];
  1276.                        sprintf(text,"Score: %d",HighScore);
  1277.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1278.                        XFillRectangle(display,window,gc,75,0,80,55);
  1279.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1280.                        XDrawString(display,window,gc,75,30 ,text,strlen(text));
  1281.                        max=X_MAX*Y_MAX;
  1282.                        for (t=0;t<=max;t++)
  1283.                        {
  1284.                          if ((Feld_Visible[t]==HIDE)) k=1;
  1285.                          if ((Feld_Intern[t]!=BOMB) && (Feld_Visible[t]==LOCK)) k=1;
  1286.                        }
  1287.                        if (k==0) 
  1288.                        {/* spiel beendet und gewonnen ! */ 
  1289.                         spiel_laeuft=0;
  1290.                        strcpy(text,"You win the game. Please RESTART NOW the game ");
  1291.                        if (Client==1) strcpy(text,"You win the game. This game will be restart by the Serverplayer now");
  1292.                        if (Server==1) strcpy(text,"You win the game. Please RESTART NOW the game ");
  1293.                         strcpy(winner,text);
  1294.                        Feld_Visible[X_MAX*Y_MAX+3]=77;
  1295.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1296.                        XFillRectangle(display,window,gc,4,385,625,400);
  1297.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1298.                        XDrawString(display,window,gc,10,395 ,text,strlen(text));
  1299.                        if (Server==1) Sende_to_Client();
  1300.                        if (Client==1) 
  1301.                          {
  1302.                            Client_to_Server();                    
  1303.                          }    
  1304.  
  1305.                     }
  1306.  
  1307.                    }
  1308.  
  1309.  
  1310.                      draw_item(x,y);
  1311.                      male_gitter();
  1312.                   }
  1313.               } else
  1314.               { /* taste 2/3 im spielfeld geklickt ! */
  1315.                   xx=x+y*X_MAX;
  1316.                   
  1317.                   if (Feld_Visible[xx]==HIDE)
  1318.                   {
  1319.                    /* hat auf verstecktes feld getippt */
  1320.                    Feld_Visible[xx]=LOCK;
  1321.                    draw_item(x,y);
  1322.                    male_gitter();
  1323.                   } else
  1324.                   { 
  1325.                   if (Feld_Visible[xx]==LOCK)
  1326.                     {
  1327.                      /* hat auf gelocktes feld getippt */
  1328.                     Feld_Visible[xx]=HIDE;               
  1329.                     draw_item(x,y);
  1330.                     male_gitter();
  1331.                     } 
  1332.                   }
  1333.                      {  /* user hat ein zahlenfeld/space aufgedeckt ! */
  1334.                        int k;
  1335.                        k=0;
  1336.                        sprintf(text,"Score: %d",HighScore);
  1337.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1338.                        XFillRectangle(display,window,gc,75,0,80,55);
  1339.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1340.                        XDrawString(display,window,gc,75,30 ,text,strlen(text));
  1341.                        max=X_MAX*Y_MAX;
  1342.                       for (t=0;t<=max;t++)
  1343.                        {
  1344.                          if ((Feld_Visible[t]==HIDE)) k=1;
  1345.                          if ((Feld_Intern[t]!=BOMB) && (Feld_Visible[t]==LOCK)) k=1;
  1346.                        }
  1347.                        if (k==0) 
  1348.                        {/* spiel beendet und gewonnen ! */ 
  1349.                        spiel_laeuft=0;
  1350.                        strcpy(text,"You win the game.");
  1351.                        strcpy(text,"You win the game. Please RESTART NOW the game ");
  1352.                        if (Client==1) strcpy(text,"You win the game. This game will be restart by the Serverplayer now");
  1353.                        if (Server==1) strcpy(text,"You win the game. Please RESTART NOW the game ");
  1354.                        strcpy(winner,text);
  1355.                        Feld_Visible[X_MAX*Y_MAX+3]=77;
  1356.                        XSetForeground(display, gc, getcolor("gray",foreground));
  1357.                        XFillRectangle(display,window,gc,4,385,625,400);
  1358.                        XSetForeground(display, gc, getcolor("blue",foreground));
  1359.                        XDrawString(display,window,gc,10,395 ,text,strlen(text));
  1360.                        if (Server==1) Sende_to_Client();
  1361.                        if (Client==1) 
  1362.                          {
  1363.                           Client_to_Server();
  1364.                          }    
  1365.  
  1366.                         }
  1367.  
  1368.                    }
  1369.  
  1370.               }
  1371.                 }
  1372.             }
  1373.             break;
  1374.  
  1375.     }
  1376.  
  1377.  
  1378. }
  1379.  
  1380.